package com.martin.lib_base.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.martin.lib_base.interfaces.ILoading
import com.martin.lib_base.interfaces.IRelease
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.utils.FishActivityCollector
import com.martin.lib_base.utils.ReleaseUtil
import getAppViewModel
import kotlin.properties.Delegates

/**
 * @Author: martin
 * @CreateDate: 2019/8/14 9:45
 * @Description:
 */
abstract class BaseActivity<T : ViewDataBinding, VM : BaseViewModel>
    : AppCompatActivity(), ILoading, IRelease {

    //视图绑定器
    protected var binding: T by Delegates.notNull()

    val viewModel: VM by lazy {
        ViewModelProvider(this)[getViewModelClass()]
    }

    // 全局ViewModel
    val globalViewModel: GlobalViewModel by lazy {
        getAppViewModel()
    }

    /**
     * Activity创建
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        FishActivityCollector.addActivity(this)
        beforeSetContentView()
        if (showLife()) {
            loge("${getClassName()} : onCreate")
        }
        //dataBinding初始化
        binding = DataBindingUtil.setContentView(this, layoutId())
        binding.lifecycleOwner = this
        //ViewModel初始化
        viewModel.initContext(requireContext(), intent)

        initOnCreate(savedInstanceState)
    }

    abstract fun getViewModelClass(): Class<VM>

    /**
     * 设置布局之前
     */
    open fun beforeSetContentView() {}

    /**
     * 布局ID
     */
    abstract fun layoutId(): Int

    /**
     * 初始化Activity内容
     */
    abstract fun initOnCreate(savedInstanceState: Bundle?)

    override fun onResume() {
        /*
            强制设置为竖屏
         */
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onResume()
        if (showLife()) {
            loge("${getClassName()} : onResume")
        }
    }

    fun requireActivity() = this

    fun requireContext() = this

    /**
     * 展示加载框
     */
    override fun showLoading() {
        if (!isFinishing) {
            viewModel.showLoading()
        }
    }

    /**
     * 隐藏加载弹窗
     */
    override fun dismissLoading() {
        viewModel.dismissLoading()
    }

    override fun onPause() {
        super.onPause()
        if (showLife()) {
            loge("${getClassName()} : onPause")
        }
    }

    override fun onStop() {
        super.onStop()
        if (showLife()) {
            loge("${getClassName()} : onStop")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        FishActivityCollector.removeActivity(this)
        if (showLife()) {
            loge("${getClassName()} : onDestroy")
        }
        release()
    }

    override fun release() {
        dismissLoading()
        ReleaseUtil.release(this)
    }

    private fun getClassName() = javaClass.simpleName

    /**
     * 展示生命周期
     */
    open fun showLife() = false
}